package cn.elricyu.kf.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceException;

import org.springframework.orm.jpa.JpaCallback;
import org.springframework.orm.jpa.support.JpaDaoSupport;

import cn.elricyu.kf.bean.SnType;
import cn.elricyu.kf.exception.DaoException;

public class SnTypeDAO extends JpaDaoSupport {

	public SnType saveSnType(SnType sntype) throws DaoException {
		try {
			getJpaTemplate().persist(sntype);
		} catch (Exception e) {
			throw new DaoException("SnTypeDAO.saveSnType", e);
		}
		return sntype;
	}

	public SnType updateSnType(SnType sntype) throws DaoException {
		try {
			getJpaTemplate().merge(sntype);
		} catch (Exception e) {
			throw new DaoException("SnTypeDAO.updateSnType", e);
		}
		return sntype;
	}

	public void deleteSnType(Integer id) throws DaoException {
		try {
			getJpaTemplate().remove(getSnType(id));
		} catch (Exception e) {
			throw new DaoException("SnTypeDAO.deleteSnType", e);
		}
	}

	@SuppressWarnings("unchecked")
	public List<SnType> getAllSnTypes() throws DaoException {
		try {
			return (List<SnType>) getJpaTemplate().execute(new JpaCallback() {
				public Object doInJpa(EntityManager em)
						throws PersistenceException {
					return em.createNamedQuery("SnType.getAllSnType")
							.setMaxResults(100).getResultList();
				}
			});
		} catch (Exception e) {
			throw new DaoException("SnTypeDAO.getAllSnTypes", e);
		}
	}

	public SnType getSnType(final Integer id) throws DaoException {
		try {
			return (SnType) getJpaTemplate().execute(new JpaCallback() {
				public Object doInJpa(EntityManager em)
						throws PersistenceException {
					return em.find(SnType.class, id);
				}
			});
		} catch (Exception e) {
			throw new DaoException("SnTypeDAO.getSnType", e);
		}
	}
	
	public SnType getSnTypeByType(final String type) throws DaoException {
		try {
			List<SnType> SnTypeList = (List<SnType>) getJpaTemplate()
					.execute(new JpaCallback() {
						public Object doInJpa(EntityManager em)
								throws PersistenceException {
							return em.createNamedQuery(
									"SnType.findSnTypeByType").setParameter(
									"type", type).getResultList();
						}
					});

			if (SnTypeList != null && SnTypeList.size() > 0) {
				return SnTypeList.get(0);
			}
			return null;
		} catch (Exception e) {
			throw new DaoException("SnTypeDAO.getSnTypeByAppName", e);
		}
	}
	
	public SnType getSnTypeByAppName(final String appName) throws DaoException {
		try {
			List<SnType> SnTypeList = (List<SnType>) getJpaTemplate()
					.execute(new JpaCallback() {
						public Object doInJpa(EntityManager em)
								throws PersistenceException {
							return em.createNamedQuery(
									"SnType.findSnTypeByAppName").setParameter(
									"appName", appName).getResultList();
						}
					});

			if (SnTypeList != null && SnTypeList.size() > 0) {
				return SnTypeList.get(0);
			}
			return null;
		} catch (Exception e) {
			throw new DaoException("SnTypeDAO.getSnTypeByAppName", e);
		}
	}

}
